package com.yanqu.road.server.cmd.activity.summergames;

import com.yanqu.road.entity.activity.summergames.SummerGamesConfig;
import com.yanqu.road.entity.activity.summergames.SummerGamesSelectReward;
import com.yanqu.road.entity.activity.summergames.SummerGamesUserData;
import com.yanqu.road.entity.activity.summergames.config.SummerGamesConvertInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SummerGamesProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.summergames.SummerGamesModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.summergames.SummerGamesActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_SUMMER_GAMES_SET_SELECT, desc = "设置自选奖励")
public class SummerGamesSetSelectRewardCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        SummerGamesConfig config = SummerGamesActivityMgr.getSummerGamesConfig();
        // 没有活动
        if (config == null || !ActivityMgr.activityInTimeAndShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        SummerGamesUserData userData = player.getModule(SummerGamesModule.class).getUserData();
        if(userData == null){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        SummerGamesProto.SummerGamesSetSelectRewardReqMsg reqMsg = SummerGamesProto.SummerGamesSetSelectRewardReqMsg.parseFrom(packet.getBytes());

        int id = reqMsg.getId();
        SummerGamesConvertInfo convertInfo = config.getSummerGamesConvertInfo(id);
        if(convertInfo == null){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        String selectReward = reqMsg.getSelect();
        Integer limit = convertInfo.getFlexiExchangeRewardMap().get(selectReward);
        if(limit == null){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        SummerGamesSelectReward summerGamesSelectReward = userData.getConvertSelectMap().get(id);
        if(summerGamesSelectReward == null){
            summerGamesSelectReward = new SummerGamesSelectReward();
            summerGamesSelectReward.setId(id);
        }
        Integer times = summerGamesSelectReward.getSelectMap().getOrDefault(selectReward, 0);
        if(times >= limit){
            ret = GameErrorCode.E_HONG_XIAN_REWARD_SELECTED_MAX_LIMIT;
            return;
        }
        summerGamesSelectReward.setSelect(selectReward);
        userData.getConvertSelectMap().put(id, summerGamesSelectReward);
        userData.setUpdateOption();

        SummerGamesProto.SummerGamesSetSelectRewardRespMsg.Builder clientMsg = SummerGamesProto.SummerGamesSetSelectRewardRespMsg.newBuilder().setRet(0);
        clientMsg.setSelectReward(SummerGamesActivityMgr.buildSummerGamesSelectRewardTemp(summerGamesSelectReward));
        player.sendPacket(Protocol.U_SUMMER_GAMES_SET_SELECT, clientMsg);

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_SUMMER_GAMES_SET_SELECT, SummerGamesProto.SummerGamesSetSelectRewardRespMsg.newBuilder().setRet(ret));
        }
    }
}
